<template>
  <div class="layout_editPassword">
    <div class="row">
      <label>手机号&emsp;&emsp;</label>
      <input type="text" v-model="phone" readonly />
      <div class="code" @click="getCode()">
        <span>{{this.code_msg}}</span>
      </div>
    </div>
    <div class="row">
      <label>验证码&emsp;&emsp;</label>
      <input type="text" v-model="code" />
    </div>
    <div class="row">
      <label>新密码&emsp;&emsp;</label>
      <input type="password" v-model="password" placeholder="请输入" />
    </div>
    <div class="row">
      <label>确认密码&emsp;</label>
      <input type="password" v-model="re_password" placeholder="请输入" />
    </div>

    <div class="tips">
      *密码必须英文字母+数字，长度为6-16
    </div>

    <!-- 修改密码 -->
    <div class="my-button">
      <Button @click.stop="edit()">修改密码</Button>
    </div>
  </div>
</template>

<script>
import api from "../../assets/js/api";
export default {
  data() {
    return {
      phone: "",
      code: "",
      password: "",
      re_password: "",

      code_msg: "获取验证码",
      lock: false
    };
  },
  created() {},
  mounted() {
    this.axios.defaults.baseURL = api;

    let phone = localStorage.getItem("phone");
    this.phone = phone;
  },
  components: {},
  props: {},
  watch: {},
  computed: {},
  methods: {
    getCode: function() {
      if (this.lock == true) {
        return false;
      }
      var tips = 60;
      var that = this;
      var timer = setInterval(function() {
        tips--;

        that.code_msg = tips + "s";
        that.lock = true;
        if (tips == 0) {
          clearInterval(timer);
          that.code_msg = "获取验证码";
          that.lock = false;
        }
      }, 1000);

      this.axios
        .post("api/butler/send_code", {})
        .then(result => {
          let res = result.data;

          if (res.code == 250) {
            this.$router.push("/login");
            return false;
          }

          if (res.code != 200) {
            return false;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

    edit: function() {
      if (this.code == "" || this.password == "" || this.re_password == "") {
        this.toastLib.commonToast("请输入必填项！");
        return false;
      }

      if (this.password != this.re_password) {
        this.toastLib.commonToast("新密码与确认密码不一致！");
        return false;
      }


      var regu = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"
      var re = new RegExp(regu)
      if (!re.test(this.password)) {
        console.log(re.test(this.password));
        this.toastLib.commonToast("密码格式错误！");
        return false;
      } 

      this.axios
        .post("api/butler/edit_password", {
          code: this.code,
          password: this.password,
          re_password: this.re_password
        })
        .then(result => {
          let res = result.data;

          if (res.code == 250) {
            this.$router.push("/login");
            return false;
          }

          if (res.code != 200) {
            this.toastLib.commonToast(res.msg);
            return false;
          }

          this.toastLib.commonToast(res.msg);
          let that = this;
          setTimeout(function() {
            that.$router.push("/login");
          }, 800);

          return false;
        })
        .catch(err => {
          console.log(err);
        });
    },
  }
};
</script>
<style lang="less" scoped>
.layout_editPassword {
  width: 100%;
  height: 100%;
  overflow: auto;
  font-size: 17px;
  color: #666;
  margin: 0 auto;

  .tips {
    margin: 0px 3%;
    // padding-left: 15px;
    font-size: 14px;
    line-height: 48px;
  }
  .row {
    margin: 0px 3%;
    border-bottom: 1px solid hsla(0, 0%, 80%, 0.2999);
    font-size: 18px;
    line-height: 48px;
    position: relative;
    padding-left: 0px;

    .code {
      position: absolute;
      font-size: 13px;
      line-height: 22px;
      margin-top: 13px;
      padding: 0px 10px;
      right: 2px;
      z-index: 10;
      border: 1px solid #ff8787;
      border-radius: 10px;
    }

    label {
      font-weight: 500;
    }
  }

  button {
    height: 48px;
    border-radius: 24px;
    background: -webkit-gradient(
      linear,
      left top,
      left bottom,
      from(#ffdddd),
      to(#ff83a6)
    );
    background: -webkit-linear-gradient(top, #ffdddd, #ff83a6);
    background: linear-gradient(180deg, #ffdddd, #ff83a6);
    color: #fff;
    font-size: 20px;
    width: 70%;
    margin: 45px 15%;
    // outline: none;
  }
  button:hover{
    // outline: none;
  }

.ivu-btn:hover {
  border-color: #ff83a6;
}

  .my-button {
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 12%;
  }
}
</style>
